<template>
  <div v-loading="loading" element-loading-text="模型生成中...">
    <div style="width: 100%; border: 1px solid white;">
      <el-form  ref="form" label-width="100px">
        <el-form-item label="雷诺数">
          <el-input size="small" v-model.number="visc" type="number" placeholder="请输入内容">
          </el-input>
        </el-form-item>
        <el-form-item label="起点攻角">
          <el-input size="small" v-model.number="startAngle" type="number" placeholder="请输入内容">
          </el-input>
        </el-form-item>
        <el-form-item label="终点攻角">
          <el-input size="small" v-model.number="endAngle" type="number" placeholder="请输入内容">
          </el-input>
        </el-form-item>
        <el-form-item label="攻角步长">
          <el-input size="small" v-model.number="stepAngle" type="number" placeholder="请输入内容">
          </el-input>
        </el-form-item>
        <el-form-item label="导入翼型数据">
          <el-upload
              ref="uploadRef"
              :auto-upload="false"
              action=""
              :on-change="handleChange"
              :file-list="fileList"
              :limit="1"
              style="margin-bottom: 20px;"
          >
            <el-button size="small" type="primary">选择文件</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div style="text-align: center; " :style="fileList.length > 0 ? 'margin-top: 40px' : 'margin-top: 10px'">
        <el-button type="primary" size="small" @click="creatWingRib">仿真计算</el-button>
        <el-button type="primary" size="small" @click="exportDatFile">导出文件</el-button>
        <el-button size="small" @click="closeDialog">取消</el-button>
      </div>
    </div>

  </div>
</template>

<script>
  import {
    Axis,
    CrownCADPlugin,
    Direction, PluginCADToolFunctionType,
    PluginFunctionType,
    Point
  } from "../../../../nodeApp/static/lib/crowncad-plugin-sdk-0.0.1";
  import {request} from "../../../util/Request";
export default {
  name: "ComputationalFluidDynamics",
  data() {
    return {
      loading: false,
      minHeight: 225,
      fileList: [], // 文件列表
      file: null,   // 当前选中的文件
      curvedSurface: null,
      radio: "2",
      visc: 500000,
      startAngle:0,
      endAngle:10,
      stepAngle:1,
    }
  },
  async mounted() {
    //创建插件对象
    this.crownCADPlugin = new CrownCADPlugin();
    //设置CrownCAD平台地址
    this.crownCADPlugin.setParentOrigin(window.url);
    //.设置与CrownCAD平台尝试建立连接的次数
    this.crownCADPlugin.setMaxAttempt(5);
    //设置插件在CrownCAD平台显示的尺寸
    this.crownCADPlugin.setLayout(320, 320, this.minHeight);
    // 发送与CrownCAD平台建立连接的请求
    await this.crownCADPlugin.connect()
    await this.crownCADPlugin.request.execFunction(PluginFunctionType.LAYOUT, { minWidth: 350, width: 350, maxHeight: this.minHeight})
    this.crownCADPlugin.command.clearCommand();

  },
  methods: {
    //关闭对话框
    closeDialog() {
      this.crownCADPlugin.request.execFunction(PluginCADToolFunctionType.CLOSEACTIVECMD, null);
    },
    async  handleChange(file) {
      this.file = file.raw; // 获取原始文件对象
      this.fileList = [file]; // 更新文件列表
    },
   // 手动上传按钮点击事件
    async handleUpload() {
      if (!this.file) {
        this.$message.error('请先选择一个文件！');
        return;
      }

      try {
        // 调用 uploadFile 方法进行上传
        await this.uploadFile(this.file);
      } catch (error) {
        this.$message.error('文件上传失败，请重试！');
      }

    },
    // 上传翼型文件
    async creatWingRib() {
      await this.handleUpload(); // 自动上传文件
    },
    async uploadFile(file) {
      try {
        const formData = new FormData();
        formData.append('file', file); // file 是从 <input type="file"> 获取的文件对象
        formData.append('visc', this.visc);
        formData.append('startAngle', this.startAngle);
        formData.append('endAngle', this.endAngle);
        formData.append('stepAngle', this.stepAngle);

        const response = await request.post('/bk/uploadDat', formData, {
          headers: {
            'Content-Type': 'multipart/form-data',
          },
        });
        console.log(response);
        this.$message.success('计算完成');
        // await this.generates()
      } catch (error) {

      }
    },

    // 导出翼型分析的数据
    async exportDatFile() {
      const formData = new FormData();
      formData.append('visc', this.visc);
      const response = await request.post('/bk/exportDatFile',formData,{
        headers: {
          'Content-Type': 'multipart/form-data',
        },
        responseType: 'blob' // 设置响应类型为blob
      });
      console.log(response.data);
      const blob = new Blob([response.data], { type: response.headers['content-type'] });
      const url = URL.createObjectURL(response.data);
      // 创建一个<a>元素来触发下载
      const a = document.createElement('a');
      a.href = url;
      a.download = 'results.zip'; // 设置下载的文件名
      document.body.appendChild(a);
      a.click();
      a.remove();
      URL.revokeObjectURL(url);
    }
  }
}
</script>

<style scoped>
>>> .el-form-item {
  margin-bottom: 5px!important;
}
>>> .el-form-item__label {
  margin-bottom: 5px!important;
}

>>> .el-form-item__content {
  display: flex!important;
  align-items: center!important;
}

>>> .el-form-item {
  height: 32px!important;
}
>>> .el-form-item__label {
  line-height: 32px !important;
}
>>> .el-upload-list__item:first-child {
  margin-top: 0 !important;
}
>>> .el-upload-list__item {
  margin-top: 0 !important;
}

</style>
